import { createRouter, createWebHashHistory } from 'vue-router'
import store from '../store/store'
import ViewUIPlus from 'view-ui-plus'
import routers from './routers'


const router = createRouter({
	history: createWebHashHistory(),
	routes: routers
})

// 导航钩子，全局钩子
router.beforeEach((to, from, next) => {
  	// 登录页面
	if (to.name === 'login') {
		if (store.getters.token == '') {
			next();
		} else {
			next('/index/index');
		}
	}
  	// 其它页面
	else {
		if (store.getters.token == '' && to.meta.requiresAuth) {
			next('/');
		} else {
			next();
		}
	}
})
router.afterEach(to => {
	ViewUIPlus.LoadingBar.finish()
})

export default router
